package com.xuexiang.xuidemo.slice.status;

import com.xuexiang.xui_lib.component.AnimatorLayout;
import com.xuexiang.xui_lib.component.TitleBarLayout;
import com.xuexiang.xui_lib.component.status.MultipleStatusComponent;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.slice.BaseAbilitySlice;
import com.xuexiang.xuidemo.util.AnimatorFactory;
import com.xuexiang.xuidemo.util.ShapeElementUtil;
import com.xuexiang.xuidemo.util.ToastUtil;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.element.ShapeElement;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

public class MultipleStatusViewSlice extends BaseAbilitySlice implements Runnable, Component.ClickedListener, AnimatorLayout.OnAnimatorChangeListener {

    private final EventHandler mLoadingHandle = new EventHandler(EventRunner.getMainEventRunner());

    private MultipleStatusComponent mStatusComponent;
    private AnimatorProperty imageAnimatorOut;
    private AnimatorProperty imageAnimator;
    private AnimatorLayout mAnimatorLayout;
    private Image roundBtn;

    @Override
    protected int getTitleBarId() {
        return ResourceTable.Id_title_bar;
    }

    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_ability_slice_multiple_status;
    }

    @Override
    protected void initComponents() {
        mStatusComponent = (MultipleStatusComponent) findComponentById(ResourceTable.Id_multiple_status_component);
        mStatusComponent.setContentComponentId(ResourceTable.Layout_status_content_layout);
        mStatusComponent.setOnRetryClickListener(this);

        setRound();
        roundBtn = (Image) findComponentById(ResourceTable.Id_round_image);
        imageAnimator = AnimatorFactory.createRotate(roundBtn, 45);
        imageAnimatorOut = AnimatorFactory.createRotate(roundBtn, 0);

        mAnimatorLayout = (AnimatorLayout) findComponentById(ResourceTable.Id_animator_layout);
        mAnimatorLayout.setOnDismissListener(this);
        mAnimatorLayout.setMenuClickListener(this::menuClick);
        roundBtn.setBackground(ShapeElementUtil.createRound(25, new RgbColor(42, 156, 220)));
        roundBtn.setClickedListener(component -> {
            if (mAnimatorLayout.isOpen()){
                mAnimatorLayout.dismissAllChild();
            }else {
                mAnimatorLayout.showAllChild();
            }
        });
        loading();
    }

    @Override
    protected void onActive() {
        super.onActive();
        if (imageAnimatorOut !=null){
            imageAnimatorOut.start();
        }
    }

    private void setRound() {
        ShapeElement round = ShapeElementUtil.createRound(25, new RgbColor(42, 156, 220));
        findComponentById(ResourceTable.Id_tag_1).setBackground(round);
        findComponentById(ResourceTable.Id_tag_2).setBackground(round);
        findComponentById(ResourceTable.Id_tag_3).setBackground(round);
        findComponentById(ResourceTable.Id_tag_4).setBackground(round);
        findComponentById(ResourceTable.Id_tag_5).setBackground(round);
    }

    private void menuClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_fb_loading:
                loading();
                break;
            case ResourceTable.Id_fb_empty:
                mStatusComponent.showEmpty();
                break;
            case ResourceTable.Id_fb_error:
                mStatusComponent.showError();
                break;
            case ResourceTable.Id_fb_net_work_error:
                mStatusComponent.showNoNetwork();
                break;
            case ResourceTable.Id_fb_comment:
                mStatusComponent.showContent();
                break;
        }
    }

    @Override
    public void onClick(Component component) {
        ToastUtil.showToast(getContext(), "点击重试");
        loading();
    }

    private void loading() {
        mStatusComponent.showLoading();
        mLoadingHandle.postTask(this, 5000L);
    }

    @Override
    public void run() {
        if (mStatusComponent.getViewStatus() == MultipleStatusComponent.STATUS_LOADING) {
            mStatusComponent.showContent();
        }
    }

    @Override
    public void onStartAnimator() {
        imageAnimator.start();
    }

    @Override
    public void onDismissAnimator() {
        imageAnimatorOut.start();
    }

}
